import { _decorator, Button, color, find, instantiate, Label, Node, NodePool, Sprite, Tween, tween, UIOpacity, UITransform, v3, Widget } from 'cc';
import { DataProxy } from '../../../frame/A-LIB/lib.b.data';
import { AudioManager } from '../../../frame/component/audio-manager';
import { BaseUI } from '../../../frame/component/ui.base';
import Config from '../../manager/config.manager';
import GameDC from '../../manager/data.game.center';
import GameAssets from '../../manager/game.assets';
import GameManager from '../../manager/game.manager';
import { Float } from './float';
import { Guide } from './guide';
import { Popup } from './popup';
const { ccclass, property } = _decorator;

@ccclass('HomeUI')
export class HomeUI extends BaseUI {

	private ui_top: UITransform;
	private ui_bottom: UITransform;
	private va_top: mtec.cc.VecAssist;
	private va_bottom: mtec.cc.VecAssist;
	private ui_top_hilit: UITransform;

	private ui_symbol: UITransform;
	private ui_diamond: UITransform;
	private lbl_symbol: Label;
	private lbl_diamond: Label;
	private icon_symbol: Node;
	private icon_diamond: Node;

	private symbol_demo: Node;
	private symbol_pool: NodePool;

	private pop_lock: boolean = false;

	protected initAfterOnLoad(){
		this.ui_top = this.node.getChildByName('top').getComponent(UITransform);
		this.ui_bottom = this.node.getChildByName('bottom').getComponent(UITransform);
		this.va_top = new mtec.cc.VecAssist(this.ui_top.node);
		this.va_bottom = new mtec.cc.VecAssist(this.ui_bottom.node);
		this.ui_top_hilit = this.node.getChildByName('top-hilit').getComponent(UITransform);
		this.ui_top_hilit.node.active = false;

		this.ui_symbol = this.ui_top.node.getChildByName('symbol').getComponent(UITransform);
		this.ui_diamond = this.ui_top.node.getChildByName('diamond').getComponent(UITransform);
		this.lbl_symbol = this.ui_symbol.node.getChildByName('lbl-value').getComponent(Label);
		this.lbl_diamond = this.ui_top.node.getChildByName('lbl-diamond').getComponent(Label);
		this.icon_symbol = find('icon/sprite', this.ui_symbol.node);
		this.icon_diamond = this.ui_diamond.node.getChildByName('icon');;

		this.symbol_demo = this.ui_top.node.getChildByName('amount-symbol');
		this.symbol_pool = new NodePool();

		this.symbol_pool.put(this.symbol_demo);

		this.renderLocalUI();
	}

	protected start(){
		let wgt = this.node.getComponent(Widget);
		if(wgt) wgt.updateAlignment();

		this.va_top.getPosition('center', 'over_top');
		this.va_bottom.getPosition('center', 'over_bottom');
		this.ui_top.node.setPosition(this.va_top.vec);
		this.ui_bottom.node.setPosition(this.va_bottom.vec);

		let reg = DataProxy.follow(GameDC.Trigger, 'LiveCommentsProgress', ()=>{
			let [f, t] = GameDC.Trigger.LiveCommentsProgress.split(',').map(v=>parseInt(v));
			let p = f/t;
			if(p>=1){
				DataProxy.cancel(reg);
				this.node.active = false;
			}
		}, false);

		let has_new_task_done = false;
		has_new_task_done = Config.taskRemove.filter(el=>!GameDC.checkTask('remove', el.count) && el.count <= GameDC.User.remove_count).length > 0;
		has_new_task_done = has_new_task_done || Config.taskRound.filter(el=>!GameDC.checkTask('round', el.round) && el.round < GameDC.User.round).length > 0;

		this.__btn_map__.get('task').node.getChildByName('hint').active = has_new_task_done;
	}

	protected _click_event_: { [name: string]: (this: HomeUI, button: Button) => void; } = {
		setting(btn){
			if(Popup.lock){
				return void 0;
			}
			Popup.lock = true;
			btn.interactable = false;
			Popup.Setting().then(()=>{
				btn.interactable = true;
				Popup.lock = false;
			});
		},
		task(btn){
			if(Popup.lock){
				return void 0;
			}
			Popup.lock = true;
			btn.interactable = false;
			Popup.Task().then(()=>{
				btn.interactable = true;
				Popup.lock = false;
			});
		},
		rank(btn){
			if(Popup.lock){
				return void 0;
			}
			Popup.lock = true;
			btn.interactable = false;
			Popup.Rank().then(()=>{
				btn.interactable = true;
				Popup.lock = false;
			});
			btn.node.getChildByName('hint').active = false;
			GameDC.User.open_rank = Date.now();
		},
		free(btn){
			if(Popup.lock){
				return void 0;
			}
			Popup.lock = true;
			btn.interactable = false;
			Popup.Free().then(()=>{
				btn.interactable = true;
				Popup.lock = false;
			});
		},
		diamond(btn){
			btn.interactable = false;
			Float.Hint(GameAssets.getLangText(82))
			.then(()=>btn.interactable = true);
		},
		async stash(btn){
			if(Popup.lock){
				return void 0;
			}
			this.enablePropBtn(false);
			Popup.lock = true;

			let count = GameDC.Status.prop_stash;
			if(count>0){
				GameDC.Trigger.STASH = false;
			}else{
				let list = GameDC.prop_geted.filter(el=>el.startsWith('stash'));
				if(list.length<2){
					GameDC.Trigger.HILIT_DIAMOND = true;
					let type = await Popup.Prop('stash', list.map(el=>el.replace('stash-', '')) as Array<any>);
					GameDC.Trigger.HILIT_DIAMOND = false;
					if(type != 'not'){
						GameDC.prop_geted.push(('stash-'+type) as any);
						count += 1;
						Float.moveProp('stash', btn.node);
						GameDC.Status.prop_stash = count;

						Float.Hint(GameAssets.getLangText(64));
					}
				}else{
					Float.Hint(GameAssets.getLangText(65));
				}
			}

			this.enablePropBtn(true);
			Popup.lock = false;
		},
		async revoke(btn){
			if(Popup.lock){
				return void 0;
			}
			this.enablePropBtn(false);
			Popup.lock = true;

			let count = GameDC.Status.prop_revoke;
			if(count>0){
				GameDC.Trigger.REVOKE = false;
			}else{
				let list = GameDC.prop_geted.filter(el=>el.startsWith('revoke'));
				if(list.length<2){
					GameDC.Trigger.HILIT_DIAMOND = true;
					let type = await Popup.Prop('revoke', list.map(el=>el.replace('revoke-', '')) as Array<any>);
					GameDC.Trigger.HILIT_DIAMOND = false;
					if(type != 'not'){
						GameDC.prop_geted.push(('revoke-'+type) as any);
						count += 1;
						Float.moveProp('revoke', btn.node);
						GameDC.Status.prop_revoke = count;

						Float.Hint(GameAssets.getLangText(64));
					}
				}else{
					Float.Hint(GameAssets.getLangText(65));
				}
			}

			this.enablePropBtn(true);
			Popup.lock = false;
		},
		async shuffle(btn){
			if(Popup.lock){
				return void 0;
			}
			this.enablePropBtn(false);
			Popup.lock = true;

			let count = GameDC.Status.prop_shuffle;
			if(count>0){
				GameDC.Trigger.SHUFFLE = false;
			}else{
				let list = GameDC.prop_geted.filter(el=>el.startsWith('shuffle'));
				if(list.length<2){
					GameDC.Trigger.HILIT_DIAMOND = true;
					let type = await Popup.Prop('shuffle', list.map(el=>el.replace('shuffle-', '')) as Array<any>);
					GameDC.Trigger.HILIT_DIAMOND = false;
					if(type != 'not'){
						GameDC.prop_geted.push(('shuffle-'+type) as any);
						count += 1;
						await Float.moveProp('shuffle', btn.node);
						GameDC.Status.prop_shuffle = count;

						Float.Hint(GameAssets.getLangText(64));
					}
				}else{
					Float.Hint(GameAssets.getLangText(65));
				}
			}

			this.enablePropBtn(true);
			Popup.lock = false;
		}
	};

	protected ClickBtn(btn: Button): void {
		if(btn.node.name.startsWith('free-round')){
			if(Popup.lock){
				return void 0;
			}
			Popup.lock = true;
			let round = parseInt(btn.node.name.replace('free-round-', ''));
			btn.interactable = false;
			this.freeCache(round).then(()=>{
				btn.interactable = true;
				Popup.lock = false;
			});
		}
	}

	protected listening(){

		this.lbl_symbol.string = Config.symbolString(GameDC.User.symbol);
		this.lbl_diamond.string = GameDC.User.diamond.toString();
		let node_hint_task = this.__btn_map__.get('task').node.getChildByName('hint');

		let hilit_symbol = this.ui_top_hilit.node.getChildByName('symbol');
		let hilit_diamond = this.ui_top_hilit.node.getChildByName('diamond');
		let lbl_symbol_hilit = hilit_symbol.getChildByName('lbl-value').getComponent(Label);
		let lbl_diamond_hilit = hilit_diamond.getChildByName('label').getComponent(Label);

		lbl_symbol_hilit.string = this.lbl_symbol.string;
		lbl_diamond_hilit.string = this.lbl_diamond.string;

		let lbl_pool = new NodePool();
		let lbl_pool_hilit = new NodePool();
		let lbl_offset = v3(0, 35, 0);

		let hilit_token_symbol: string;
		let hilit_token_diamond: string;

		let [icon_add_stash, icon_add_revoke, icon_add_shuffle] = ['stash', 'revoke', 'shuffle'].map(name=>this.__btn_map__.get(name).node.getChildByName('icon-add'));
		let [lbl_stash, lbl_revoke, lbl_shuffle] = ['stash', 'revoke', 'shuffle'].map(name=>this.ui_bottom.node.getChildByName('lbl-'+name+'-value').getComponent(Label));

		let diamond_icon_copy = this.icon_diamond.parent.getChildByName('copy');

		let color_dr = color(99, 243, 44);
		let color_dd = color(251, 66, 69);

		let lbl_obj_diamond = {
			__diamond__: 0,
			diamond: 0,
		};
		let lbl_obj_symbol = {
			__symbol__: 0,
			symbol: 0,
		}
		lbl_obj_diamond.__diamond__ = GameDC.User.diamond;
		lbl_obj_symbol.__symbol__ = GameDC.User.symbol;

		((o: any, lbls: Label[])=>{
			Reflect.defineProperty(o, 'diamond', {
				get() {
					return this.__diamond__;
				},
				set(v: number) {
					this.__diamond__ = v;
					lbls.forEach(l=>l.string = Math.round(v).toString());
				}
			});
		})(lbl_obj_diamond, [this.lbl_diamond, lbl_diamond_hilit]);

		((o: any, lbls: Label[])=>{
			Reflect.defineProperty(o, 'symbol', {
				get() {
					return this.__symbol__;
				},
				set(v: number) {
					this.__symbol__ = v;
					lbls.forEach(l=>l.string = Config.symbolString(v));
				}
			});
		})(lbl_obj_symbol, [this.lbl_symbol, lbl_symbol_hilit]);

		let list = [
			DataProxy.follow(GameDC.Trigger, 'UIHome', (_, show)=>{
				if(_==show) return void 0;
				if(show) this.enter();
				else this.leave();
			}, false),
			DataProxy.follow(GameDC.Trigger, 'CHANGE_SYMBOL', async (_, num)=>this.changeSymbol(num), false),
			DataProxy.follow(GameDC.Trigger, 'CHANGE_DIAMOND', async (_, num)=>{
				let vec = GameManager.reapDiamondVec;
				if(num>0 && vec){
					await Float.Reap(diamond_icon_copy, mtec.number.random(6, 8), vec);
				}

				let lbls = [{
					lbl: (lbl_pool.get() ?? instantiate(this.lbl_diamond.node)).getComponent(Label),
					pool: lbl_pool
				}];

				if(hilit_diamond.active){
					lbls.push({
						lbl: (lbl_pool_hilit.get() ?? instantiate(lbl_diamond_hilit.node)).getComponent(Label),
						pool: lbl_pool_hilit
					});
				}

				lbls.forEach(({lbl, pool})=>{
					lbl.color = num>0 ? color_dr : color_dd;
					lbl.string = (num > 0 ? '+ ' : '- ')+Math.abs(num);
					lbl.node.scale.set(0.8, 0.8, 1);
					lbl.node.active = true;
					lbl.node.setParent(lbl.node.parent);
					lbl.node.setPosition(lbl.node.position);

					tween(lbl.node).by(0.6, {position: lbl_offset}).delay(0.6)
					.call(()=>pool.put(lbl.node)).start();
				});
			}, false),
			DataProxy.follow(GameDC.Trigger, 'HILIT_SYMBOL', (_, light)=>{
				hilit_symbol.active = light;
				if(light){
					hilit_token_symbol = Float.heightLight([hilit_symbol]);
				}else{
					Float.releaseHILIT(hilit_token_symbol);
				}
			}, false),
			DataProxy.follow(GameDC.Trigger, 'HILIT_DIAMOND', (_, light)=>{
				hilit_diamond.active = light;
				if(light){
					hilit_token_diamond = Float.heightLight([hilit_diamond]);
				}else{
					Float.releaseHILIT(hilit_token_diamond);
				}
			}, false),
			DataProxy.follow(GameDC.User, 'round', ()=>{
				let has_new_task_done = Config.taskRound.filter(el=>!GameDC.checkTask('round', el.round) && el.round < GameDC.User.round).length > 0;
				has_new_task_done = has_new_task_done || Config.taskRemove.filter(el=>!GameDC.checkTask('remove', el.count) && el.count <= GameDC.User.remove_count).length > 0;

				node_hint_task.active = has_new_task_done;
			}, false),
			DataProxy.follow(GameDC.User, 'remove_count', ()=>{
				let has_new_task_done = Config.taskRemove.filter(el=>!GameDC.checkTask('remove', el.count) && el.count <= GameDC.User.remove_count).length > 0;
				has_new_task_done = has_new_task_done || Config.taskRound.filter(el=>!GameDC.checkTask('round', el.round) && el.round < GameDC.User.round).length > 0;

				node_hint_task.active = has_new_task_done;
			}, false),
			DataProxy.follow(GameDC.User, 'task_round', ()=>{
				let has_new_task_done = Config.taskRound.filter(el=>!GameDC.checkTask('round', el.round) && el.round < GameDC.User.round).length > 0;
				has_new_task_done = has_new_task_done || Config.taskRemove.filter(el=>!GameDC.checkTask('remove', el.count) && el.count <= GameDC.User.remove_count).length > 0;

				node_hint_task.active = has_new_task_done;
			}, false),
			DataProxy.follow(GameDC.User, 'task_remove', ()=>{
				let has_new_task_done = Config.taskRemove.filter(el=>!GameDC.checkTask('remove', el.count) && el.count <= GameDC.User.remove_count).length > 0;
				has_new_task_done = has_new_task_done || Config.taskRound.filter(el=>!GameDC.checkTask('round', el.round) && el.round < GameDC.User.round).length > 0;

				node_hint_task.active = has_new_task_done;
			}, false),
			this.follow(GameDC.Status, 'prop_stash', (_, v)=>{
				let empty = !(v>0);
				icon_add_stash.active = empty;
				lbl_stash.node.active = !empty;
				if(!empty) lbl_stash.string = String(v);
			}),
			this.follow(GameDC.Status, 'prop_revoke', (_, v)=>{
				let empty = !(v>0);
				icon_add_revoke.active = empty;
				lbl_revoke.node.active = !empty;
				if(!empty) lbl_revoke.string = String(v);
			}),
			this.follow(GameDC.Status, 'prop_shuffle', (_, v)=>{
				let empty = !(v>0);
				icon_add_shuffle.active = empty;
				lbl_shuffle.node.active = !empty;
				if(!empty) lbl_shuffle.string = String(v);
			}),
			this.follow(GameDC.User, 'symbol', (_, v)=>{
				//this.lbl_symbol.string = Config.symbolString(v);
				Tween.stopAllByTarget(lbl_obj_symbol);

				tween(lbl_obj_symbol)
				.to(0.8, {symbol: v})
				.start();
			}, false),
			this.follow(GameDC.User, 'diamond', (_, v)=>{
				Tween.stopAllByTarget(lbl_obj_diamond);

				tween(lbl_obj_diamond)
				.to(0.8, {diamond: v})
				.start();
			}, false),
			this.follow(GameDC.Trigger, 'REFRESH_FREE_CACHE', ()=>{
				this.refreshFreeCache();
			})
		];
		return list;
	}

	private enterAfter(){
		if(GameDC.User.round>=4) GameDC.Trigger.UILuckyBox = true;
		this.initGuide();
	}

	private enter(duration?: number){
		this.node.active = true;
		duration = duration ?? 0.5;

		this.va_top.getPosition('center', 'top');
		if(mtec.cc.is_long_screen) this.va_top.vec.add3f(0, -100, 0);

		this.ui_top_hilit.node.setPosition(this.va_top.vec);
		Tween.stopAllByTarget(this.ui_top.node);
		tween(this.ui_top.node)
		.to(duration, {position: this.va_top.vec}, {easing: 'cubicOut'})
		.call(()=>this.enterAfter())
		.start();

		this.va_bottom.getPosition('center', 'bottom');
		Tween.stopAllByTarget(this.ui_bottom.node);
		tween(this.ui_bottom.node)
		.to(duration, {position: this.va_bottom.vec}, {easing: 'cubicOut'})
		.start();
	}

	private leave(duration?: number){
		duration = duration ?? 0.5;

		this.va_top.getPosition('center', 'over_top');

		Tween.stopAllByTarget(this.ui_top.node);
		tween(this.ui_top.node)
		.to(duration, {position: this.va_top.vec}, {easing: 'cubicIn'})
		.call(()=>this.node.active = false)
		.start();

		this.va_bottom.getPosition('center', 'over_bottom');
		Tween.stopAllByTarget(this.ui_bottom.node);
		tween(this.ui_bottom.node)
		.to(duration, {position: this.va_bottom.vec}, {easing: 'cubicIn'})
		.start();

		if(GameDC.User.round) GameDC.Trigger.UILuckyBox = false;
	}

	private renderLocalUI(){
		let sframe_symbol = GameAssets.getCounTexture(6);
		let scale = mtec.size.innerRatio(sframe_symbol.originalSize, this.icon_symbol.parent.getComponent(UITransform).contentSize);
		(<const>[
			[this.ui_top, this.ui_symbol, this.icon_symbol, this.symbol_demo],
			[this.ui_top_hilit, this.ui_top_hilit.node.getChildByName('symbol').getComponent(UITransform), find('symbol/icon/sprite', this.ui_top_hilit.node), this.ui_top_hilit.node.getChildByName('amount-symbol')]
		]).forEach(([ui_top, ui, icon, demo])=>{
			icon.getComponent(Sprite).spriteFrame = sframe_symbol;
			icon.getComponent(UITransform).setContentSize(sframe_symbol.originalSize.width * scale, sframe_symbol.originalSize.height * scale);
			mtec.cc.skinPeeler(demo.getChildByName('icon'), sframe_symbol, {width: true});
			ui.node.getChildByName('lbl-free').getComponent(Label).string = GameAssets.getLangText(6);
			ui.node.getChildByName('light').active = false;

			let bubble = ui_top.node.getChildByName('bubble');
			bubble.getChildByName('label').getComponent(Label).string = GameAssets.getLangText(22);
			bubble.active = false;
		});

		this.__btn_map__.get('rank').node.getChildByName('hint').active = !mtec.time.sameDay(Date.now(), GameDC.User.open_rank);

		(<const>[
			['stash', GameAssets.getLangText(10)],
			['revoke', GameAssets.getLangText(11)],
			['shuffle', GameAssets.getLangText(12)]
		]).forEach(([name, text])=>this.ui_bottom.node.getChildByName('lbl-'+name).getComponent(Label).string = text);

		this.ui_top.node.getChildByName('free-cache').children.forEach(node=>{
			node.getChildByName('label').getComponent(Label).string = GameAssets.getLangText(19, node.name.replace('free-round-', ''));
		});
	}

	private symbol_pool_hilit = new NodePool();
	private async changeSymbol(amount: number){
		let vec = GameManager.reapSymbolVec;

		let list = [{
			ui: this.ui_symbol,
			ui_top: this.ui_top,
			lbl: this.lbl_symbol,
			pool: this.symbol_pool,
			symbol_demo: this.symbol_demo,
			icon: this.icon_symbol
		}];

		if(this.ui_top_hilit.node.active){
			list.push({
				ui: this.ui_top_hilit.node.getChildByName('symbol').getComponent(UITransform),
				ui_top: this.ui_top_hilit,
				lbl: this.ui_top_hilit.node.getChildByName('symbol').getChildByName('lbl-value').getComponent(Label),
				pool: this.symbol_pool_hilit,
				symbol_demo: this.ui_top_hilit.node.getChildByName('symbol').getChildByName('amount-symbol'),
				icon: this.ui_top_hilit.node.getChildByName('symbol').getChildByName('icon')
			});
		}

		let scale = v3(1.2, 1.2, this.ui_symbol.node.scale.z);
		let one_scale = v3(1, 1, this.ui_symbol.node.scale.z);

		list.forEach(cache=>{
			let light = cache.ui.node.getChildByName('light');
			let bubble = cache.ui_top.node.getChildByName('bubble');
			if(vec) Float.Reap(cache.icon, mtec.number.random(6, 8), vec, (n, i)=>{
				AudioManager.playEffect(AudioManager.EAudio.EF_SYMBOL);
				Tween.stopAllByTarget(cache.ui.node);
				cache.ui.node.setScale(one_scale);
				tween(cache.ui.node).to(0.1, {scale}).to(0.1, {scale: one_scale}).start();

				if(!light.active){
					light.angle = 0;
					light.active = true;
					Tween.stopAllByTarget(light);

					tween(light)
					.by(1, {angle: 180})
					.repeatForever()
					.start();
				}

				if(!bubble.active){
					bubble.active = true;
					Tween.stopAllByTarget(bubble);
					bubble.setPosition(215, 153, bubble.position.z);
					tween(bubble)
					.to(0.3, {position: v3(245, 153, bubble.position.z)}, {easing: 'cubicOut'})
					.to(0.3, {position: v3(215, 153, bubble.position.z)}, {easing: 'cubicIn'})
					.union().repeat(3).delay(0.6)
					.union().repeat(3).call(()=>{
						Tween.stopAllByTarget(bubble);
						bubble.active = false;
					}).start();
				}
			}).then(()=>{
				let opt = (cache.pool.get() ?? instantiate(cache.symbol_demo)).getComponent(UIOpacity);

				opt.node.getChildByName('lbl-value').getComponent(Label).string = '+' + Config.symbolString(amount);
				opt.node.setParent(this.ui_symbol.node);
				[opt, opt.node].forEach(el=>Tween.stopAllByTarget(el));

				opt.opacity = 255;
				opt.node.setPosition(-90, 0);

				tween(opt).delay(0.3).to(0.7, {opacity: 0}).start();

				tween(opt.node).by(1, {position: v3(-90, 115, 0)})
				.call(()=>cache.pool.put(opt.node))
				.start();

				if(light.active){
					Tween.stopAllByTarget(light);
					light.active = false;
					light.angle = 0;
				}
			});
		});
	}

	private enablePropBtn(enable: boolean){
		['stash', 'revoke', 'shuffle'].forEach(name=>this.__btn_map__.get(name).interactable = enable);
	}

	private refreshFreeCache(){
		let list = GameDC.FreePool;
		[1, 2, 3].forEach(round=>{
			this.__btn_map__.get('free-round-'+round).node.active = list.findIndex(el=>el.round==round) >= 0;
		});
	}

	private async freeCache(round: number){
		let cache = GameDC.FreePool.find(el=>el.round==round);
		if(!cache){
			return void 0;
		}

		await Popup.FreeCache(cache);
	}

	private async initGuide(){
		let btn_free = this.__btn_map__.get('free');
		let ui_guide_symbol = this.ui_top_hilit.node.getChildByName('guide-symbol').getComponent(UITransform);
		let ui_btn_free = btn_free.getComponent(UITransform);

		await GameDC.guideInited;

		if(!GameDC.checkGuideID(5)){
			Guide.Create(5,
				GameAssets.getLangText(5),
				ui_guide_symbol,
				ui_btn_free
			);

			Guide.Wait(4, false).then(()=>Guide.Show(5, 1));

			Guide.Wait(5, true).then(()=>{
				btn_free.interactable = false;
				Popup.Free().then(()=>btn_free.interactable = true);
			});
		}

		if(!GameDC.checkGuideID(6)){
			Guide.Create(6,
				GameAssets.getLangText(5),
				ui_guide_symbol,
				ui_btn_free
			);
		}

		if(GameDC.User.round>1){
			let np_guide7 = new mtec.NudityPromise<'done'|'create'>();

			if(!GameDC.checkGuideID(7)){
				let np_guide7_create = new mtec.NudityPromise<'end'>();
				if(GameDC.Trigger.FreeCacheGuide==undefined){
					let reg = this.follow(GameDC.Trigger, 'FreeCacheGuide', ()=>{
						this.cancel(reg);
						np_guide7_create.resolve('end');
					}, false);
				}else{
					np_guide7_create.resolve('end');
				}

				np_guide7_create.promise.then(()=>{
					let need_create = GameDC.Trigger.FreeCacheGuide;

					if(need_create){
						let uitrans = this.ui_top.node.getChildByName('free-cache').children.filter(n=>n.active)[0].getComponent(UITransform);
						Guide.Create(7,
							GameAssets.getLangText(5),
							uitrans,
							uitrans
						);
						let round = parseInt(uitrans.node.name.replace('free-round-', ''));
						Guide.Wait(6, true).then(()=>Guide.Show(7, 1));
						Guide.Wait(7, true)
						.then(()=>Popup.FreeCache(GameDC.FreePool.find(el=>el.round==round)))
						.then(()=>np_guide7.resolve('create'));
					}else{
						GameDC.DoneGuideID(7);
						np_guide7.resolve('done');
					}
					//Guide.Wait(7, false).then(()=>np_guide7.resolve('end'));
				});
			}else{
				np_guide7.resolve('create');
			}

			if(!GameDC.checkGuideID(8)){
				Guide.Create(8,
					GameAssets.getLangText(4),
					this.ui_bottom.node.getChildByName('guide-prop').getComponent(UITransform),
					null
				);

				np_guide7.promise.then(type=>{
					Guide.Wait(7, false).then(()=>Guide.Show(8, 1, 2));
				});
			}
		}
	}
}

